package com.ruoyi.web.controller.business;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.DelFlag;
import com.ruoyi.system.domain.ShouCang;
import com.ruoyi.system.service.IShouCangService;
import org.springframework.web.bind.annotation.*;
import org.springframework.stereotype.Controller;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;

/**
 * <p>
 * 订单详情信息 前端控制器
 * </p>
 *
 * @author qubaofeng
 * @since 2025-02-16
 */
@RestController
@RequestMapping("/system/shouCang")
public class ShouCangController extends BaseController {

    @Resource
    private IShouCangService shouCangService;

    @GetMapping("/add")
    public AjaxResult add(@RequestParam(value = "id") Long huoPinId){

        ShouCang shouCang = new ShouCang();
        shouCang.setHuoPinId(huoPinId);
        shouCang.setUserId(getUserId());
        shouCang.setCreateBy(getUsername());
        shouCang.setCreateTime(new Date());

        shouCang.setDelFlag(DelFlag.NOMAL.getCode());

        boolean ok = shouCangService.saveShouCang(shouCang);
        if(ok){
            return AjaxResult.success();
        } else {
            return AjaxResult.error();
        }

    }

    @DeleteMapping("/remove")
    public AjaxResult remove(@RequestParam(value = "id") Long huoPinId){

        ShouCang shouCang = new ShouCang();
        shouCang.setHuoPinId(huoPinId);
        shouCang.setUserId(getUserId());
        shouCang.setDelFlag(DelFlag.DEL.getCode());


        boolean ok = shouCangService.removeShouCang(shouCang);

        if(ok){
            return AjaxResult.success();
        } else {
            return AjaxResult.error();
        }
    }


    @GetMapping("/getByUserId")
    public AjaxResult getByUserId(){
        AjaxResult result = AjaxResult.success();

        LambdaQueryWrapper<ShouCang> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(ShouCang::getDelFlag, DelFlag.NOMAL.getCode());
        wrapper.eq(ShouCang::getUserId, getUserId());

        List<ShouCang> list = shouCangService.list(wrapper);

        List<Long> ids = list.stream().map(ShouCang::getHuoPinId).collect(Collectors.toList());

        result.put(AjaxResult.DATA_TAG, ids);

        return result;
    }

}
